import {
  Controller,
  Param,
  Post,
  UploadedFile,
  UseGuards,
  UseInterceptors,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { GqlAuthGuard } from '../../guards/auth.guard';
import { UserService } from '../user/user.service';
import { HOST, PORT } from '../../constant';
import { StudentService } from '../student/student.service';

@Controller('upload')
@UseGuards(GqlAuthGuard)
export class UploadController {
  constructor(
    private readonly userService: UserService,
    private readonly studentService: StudentService,
  ) {}

  @Post(':id/:isStudent')
  @UseInterceptors(FileInterceptor('file'))
  async upload(
    @UploadedFile() file: Express.Multer.File,
    @Param('id') id: string,
    @Param('isStudent') isStudent: string,
  ) {
    const imgName = `http://${HOST}:${PORT}/static/${file.filename}`;
    if (isStudent === 'true') {
      return await this.studentService.updateStudent(id, { avatar: imgName });
    } else return await this.userService.updateUser(id, { avatar: imgName });
  }
}
